#include "stdafx.h"
#include "Bin.h"

Bin::Bin(Bin* bin){
	len=bin->len;
	buf=new ubyte[len];
	memcpy(buf,bin->buf,len);
}
Bin::Bin(uint length){
	len=length;
	buf=new ubyte[length];
}
Bin::Bin(void* data,uint length){
	len=length;
	buf=new ubyte[length];
	memcpy(buf,data,length);
}
Bin::~Bin(){
	if(buf)
		delete[] buf;
}

uint Bin::checkSum(void){
	uint res=0;
	for(ubyte *e=buf+len,*p=buf;p<e;)
		res+=*p++;
	return res;
}
void Bin::copy(void* dest,uint offset,uint length){
	if((offset+length)<len)
		memcpy(dest,buf+offset,length);
	else if(offset<len){
		uint n=len-offset;
		memcpy(dest,buf+offset,n);
		memset((ubyte*)dest+n,0,length-n);
	}
}